//
//  GoodClassViewController.m
//  danpin
//
//  Created by chuangjia on 29/8/15.
//  Copyright (c) 2015 chuangjia. All rights reserved.
//

#import "GoodClassViewController.h"
#import "GoodBrandViewController.h"
#import "GoodViewController.h"
#import "CommentViewController.h"
#import "CellConfig.h"
#import "BaseModel.h"
#import "ZToastManager.h"
#import "SearchViewController.h"
@interface GoodClassViewController ()<UITableViewDataSource,UITableViewDelegate>
{
}
@property (nonatomic,strong)NSMutableDictionary *categoryDictionary;
@property(nonatomic,strong)NSMutableArray *sectionsArray;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,strong)NSMutableArray *othersArray;
@property(nonatomic,strong)NSMutableArray *tagsArray;
@property(nonatomic,strong)NSMutableArray *brandsArray;
@property(nonatomic,strong)UITableView *tbView;
@property(nonatomic,assign)int currentPage;
@property(nonatomic,assign)int morePage;
@property(nonatomic,assign)BOOL isLoadFinished;
@property(nonatomic,assign)BOOL isRequesting;
@property(nonatomic,assign)BOOL isSelectedTag;
@end

@implementation GoodClassViewController
@synthesize cateCid=_cateCid;
@synthesize cateTag=_cateTag;
@synthesize cateName=_cateName;
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =[UIColor whiteColor];
    [UMengShare event:CK_GOODCLASS acc:[_cateCid integerValue]];
    if (!_sectionsArray) {
        _sectionsArray =[[NSMutableArray alloc] init];
    }
    if (!_dataArray) {
        _dataArray=[[NSMutableArray alloc] init];
    }
    if (!_othersArray) {
        _othersArray =[[NSMutableArray alloc] init];
    }
    
    if (!_tagsArray) {
        _tagsArray =[[NSMutableArray alloc] init];
    }
    if (!_brandsArray) {
        _brandsArray =[[NSMutableArray alloc] init];
    }
    if (!_categoryDictionary) {
        _categoryDictionary =[NSMutableDictionary dictionary];
    }
    self.currentPage=0;
    self.morePage =0;
    
    self.isComment = YES;
    self.navTitle =self.cateName;
    
    self.isLoadFinished =NO;
    self.isRequesting =NO;
   
    [self notificationManager];
    self.isSelectedTag =NO;
    [self initUI];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.enableBackGesture=YES;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    DLog(@"%s", __PRETTY_FUNCTION__);
}
-(void)dealloc
{
    [_sectionsArray removeAllObjects];
    _sectionsArray=nil;
    [_dataArray removeAllObjects];
    _dataArray =nil;
    
    [_tagsArray removeAllObjects];
    _tagsArray=nil;
    _categoryDictionary=nil;
    [_othersArray removeAllObjects];
    _othersArray=nil;
    self.tbView =nil;
    DLog(@"%s", __PRETTY_FUNCTION__);
}
-(void)initUI
{
    _tbView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.navHeight, self.view.frame.size.width, self.view.frame.size.height-self.navHeight) style:UITableViewStyleGrouped];
    _tbView.delegate = self;
    _tbView.dataSource = self;
    _tbView.backgroundColor = [UIColor whiteColor];
    _tbView.showsVerticalScrollIndicator = NO;
    _tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tbView];
    
    __weak typeof (self) wSelf = self;
    [_tbView addInfiniteScrollingWithActionHandler:^{
        
        if (wSelf.isLoadFinished) {
            //停止菊花
            [wSelf.tbView.infiniteScrollingView stopAnimating];
            [wSelf.tbView setShowsInfiniteScrolling:NO];
            return;
        }
        
        if ([wSelf.othersArray count]>0) {
            wSelf.currentPage =(int)[wSelf.othersArray count]+1;
        }
        [wSelf.tbView.infiniteScrollingView startAnimating];
        [wSelf getCategoryDataWithCid:wSelf.cateCid tagstr:wSelf.cateTag];
        
    }];
//    [ZShare showAnimationView:self.view center:CGPointZero];
    [self getCategoryDataWithCid:self.cateCid tagstr:self.cateTag];
    
}

#pragma mark - tableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.sectionsArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *sectionTitle =[self.sectionsArray objectAtIndex:section];
    if ([sectionTitle isEqualToString:@"search"]) {
        return 1;
    }else
        if ([sectionTitle isEqualToString:@"cate_tags"]) {
            return 1;
        }else if ([sectionTitle isEqualToString:@"cate_brands"]) {
            return 1;
        }else if ([sectionTitle isEqualToString:@"others"]) {
            return [self.othersArray count];
        }else
        {
            id tag =[self.categoryDictionary objectForKey:sectionTitle];
            if(tag && [tag isKindOfClass:[NSDictionary class]])
            {
                NSArray *keyArry =[tag allKeys];
                if ([keyArry containsObject:@"99999999"]) {
                    int end = [[NSString stringWithFormat:@"%@",[[tag  objectForKey:@"99999999"] objectForKey:@"end"]] intValue];
                    if (end==1) {
                        return [keyArry count]-1;
                    }
                }
                return [keyArry count];
            }
        }
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    NSString *sectionTitle =[self.sectionsArray objectAtIndex:section];
    if ([sectionTitle isEqualToString:@"search"]) {
        return PHOTO_FRAME_WIDTH;
    }else
        if ([sectionTitle isEqualToString:@"cate_tags"]||[sectionTitle isEqualToString:@"cate_brands"])
        {
            return 0.01;
        }if ([sectionTitle isEqualToString:@"others"]) {
            if (self.categoryDictionary && self.categoryDictionary.count>0) {
                return PHOTO_FRAME_WIDTH*5;
            }
            return 0.01;
        }
        else
        {
            return PHOTO_FRAME_WIDTH*4;
        }
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    NSString *sectionTitle =[self.sectionsArray objectAtIndex:section];
    
    if ([sectionTitle isEqualToString:@"cate_tags"]||[sectionTitle isEqualToString:@"cate_brands"])
    {
        return PHOTO_FRAME_WIDTH*2;
    }
    return PHOTO_FRAME_WIDTH;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *sectionTitle =[self.sectionsArray objectAtIndex:indexPath.section];
    if ([sectionTitle isEqualToString:@"search"]) {
        return [SearchCell returnCellHeight:nil];
    }else if ([sectionTitle isEqualToString:@"cate_tags"]) {
        return [CategoryTagCell returnCellHeight:self.tagsArray];
    }else if ([sectionTitle isEqualToString:@"cate_brands"]) {
        return [CategoryBrandTagCell returnCellHeight:self.brandsArray];
    }else if ([sectionTitle isEqualToString:@"others"]) {
        if (self.categoryDictionary&&self.categoryDictionary.count>0)
        {
            NSDictionary *dict =[self.othersArray objectAtIndex:indexPath.row];
            return [ProductCellV1 returnCellHeight:dict];
        }else{
            NSDictionary *dict =[self.othersArray objectAtIndex:indexPath.row];
            return [ProductCellV1 returnCellHeight:dict];
        }
        
    }else
    {
        id tag =[self.categoryDictionary objectForKey:sectionTitle];
        if(tag && [tag isKindOfClass:[NSDictionary class]])
        {
            NSMutableArray *numArray = [NSMutableArray arrayWithArray:[tag allKeys]];
            [numArray sortUsingComparator: ^NSComparisonResult (NSString *str1, NSString *str2) {
                NSNumber *number1 = [NSNumber numberWithInteger:str1.floatValue];
                NSNumber *number2 = [NSNumber numberWithInteger:str2.floatValue];
                return [number1 compare:number2];
            }];
            if ([numArray containsObject:@"99999999"]) {
                int end = [[NSString stringWithFormat:@"%@",[[tag  objectForKey:@"99999999"] objectForKey:@"end"]] intValue];
                if (end==1) {
                    [numArray removeObject:@"99999999"];
                }
            }
            NSString* kv =[numArray objectAtIndex:indexPath.row];
            
            if ([kv isEqualToString:@"99999999"]) {
                return 60;
            }else {
                NSDictionary* dict =[tag objectForKey:kv];
                return [ProductCellV1 returnCellHeight:dict];
            }
        }
        
        return 0;
    }
    
    return 0;
    
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSString *sectionTitle =[self.sectionsArray objectAtIndex:section];
    UIView *sectionview =[[UIView alloc] initWithFrame:CGRectMake(0, 0, _tbView.frame.size.width, PHOTO_FRAME_WIDTH)];
    sectionview.backgroundColor=[UIColor clearColor];
    if ([sectionTitle isEqualToString:@"search"]||[sectionTitle isEqualToString:@"cate_tags"]||[sectionTitle isEqualToString:@"cate_brands"])
    {
        return nil;
    }else
        if ([sectionTitle isEqualToString:@"others"]) {
            if (self.categoryDictionary&&self.categoryDictionary.count>0) {
                UIView *sectionView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, _tbView.frame.size.width, PHOTO_FRAME_WIDTH*4)];
                sectionView.backgroundColor =[UIColor clearColor];
                UILabel *tagValue =[[UILabel alloc] initWithFrame:CGRectMake(0,PHOTO_FRAME_WIDTH+2, _tbView.frame.size.width, PHOTO_FRAME_WIDTH*2)];
                tagValue.backgroundColor = [UIColor clearColor];
                tagValue.textColor = NICKNAME_COLOR;
                tagValue.textAlignment = NSTextAlignmentCenter;
                tagValue.font = FONT(20);
                NSString *str =[NSString stringWithFormat:@"「 %@ 」",@"其他"];
                tagValue.text =str;
                [sectionView addSubview:tagValue];
                return sectionView;
            }
            return sectionview;
        }else
        {
            UIView *sectionView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, _tbView.frame.size.width, PHOTO_FRAME_WIDTH*4)];
            sectionView.backgroundColor =[UIColor clearColor];
            
            UILabel *tagValue =[[UILabel alloc] initWithFrame:CGRectMake(0, PHOTO_FRAME_WIDTH+2, _tbView.frame.size.width, PHOTO_FRAME_WIDTH*2)];
            tagValue.backgroundColor = [UIColor clearColor];
            tagValue.textColor = NICKNAME_COLOR;
            tagValue.textAlignment = NSTextAlignmentCenter;
            tagValue.font = FONT(20);
            NSString *str =[NSString stringWithFormat:@"「 %@ 」",sectionTitle];
            tagValue.text =str;
            [sectionView addSubview:tagValue];
            tagValue=nil;
            return sectionView;
        }
    
    return sectionview;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat sectionHeaderHeight = PHOTO_FRAME_WIDTH;
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = (UITableViewCell*)[tableView  dequeueReusableCellWithIdentifier:@""];
    NSString *sectionTitle =[self.sectionsArray objectAtIndex:indexPath.section];
    if ([sectionTitle isEqualToString:@"search"]) {
        SearchCell *cell = (SearchCell*)[tableView  dequeueReusableCellWithIdentifier:@""];
        if(cell == nil)
        {
            cell = [[SearchCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
            cell.backgroundColor=[UIColor clearColor];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            __weak typeof (self) wSelf = self;
            [cell setCallBack:^(BEventType t, UIView *v,id obj,id key,NSIndexPath *indxPath) {
                [wSelf cellAction:t views:v obj:obj key:key indexPath:indexPath];
            }];
        }
        [cell showInfo:@"index" indexPath:indexPath];
        return cell;
    }else if ([sectionTitle isEqualToString:@"cate_tags"]) {
        CategoryTagCell *cell = (CategoryTagCell*)[tableView  dequeueReusableCellWithIdentifier:@""];
        if(cell == nil)
        {
            cell = [[CategoryTagCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
            cell.backgroundColor=[UIColor clearColor];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            __weak typeof (self) wSelf = self;
            [cell setCallBack:^(BEventType t, UIView *v,id obj,id key,NSIndexPath *indxPath) {
                [wSelf cellAction:t views:v obj:obj key:key indexPath:indexPath];
            }];
        }
        [cell showInfo:_tagsArray indexPath:indexPath];
        return cell;
    }else if ([sectionTitle isEqualToString:@"cate_brands"]) {
        CategoryBrandTagCell *cell = (CategoryBrandTagCell*)[tableView  dequeueReusableCellWithIdentifier:@""];
        if(cell == nil)
        {
            cell = [[CategoryBrandTagCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
            cell.backgroundColor=[UIColor clearColor];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            __weak typeof (self) wSelf = self;
            [cell setCallBack:^(BEventType t, UIView *v,id obj,id key,NSIndexPath *indxPath) {
                [wSelf cellAction:t views:v obj:obj key:key indexPath:indexPath];
            }];
        }
        [cell showInfo:_brandsArray indexPath:indexPath];
        return cell;
    }else if ([sectionTitle isEqualToString:@"others"]) {
        if (self.categoryDictionary&&self.categoryDictionary.count>0)
        {
            ProductCellV1 *cell = (ProductCellV1*)[tableView  dequeueReusableCellWithIdentifier:@""];
            if(cell == nil)
            {
                cell = [[ProductCellV1 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
                
                __weak typeof (self) wSelf = self;
                [cell setCallBack:^(BEventType t, UIView *v,id obj,id key,NSIndexPath *indxPath) {
                    [wSelf cellAction:t views:v obj:obj key:key indexPath:indexPath];
                }];
            }
            if (indexPath.row >self.othersArray.count) {
                return cell;
            }
            NSDictionary *dict =[self.othersArray objectAtIndex:indexPath.row];
            [cell showInfo:dict key:@"others" indexPath:indexPath];
            return cell;
        }else{
            NSDictionary *dict =[self.othersArray objectAtIndex:indexPath.row];
            ProductCellV1 *cell = (ProductCellV1*)[tableView  dequeueReusableCellWithIdentifier:@""];
            if(cell == nil)
            {
                cell = [[ProductCellV1 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
                
                __weak typeof (self) wSelf = self;
                [cell setCallBack:^(BEventType t, UIView *v,id obj ,id key,NSIndexPath *indxPath) {
                    [wSelf cellAction:t views:v obj:obj key:key indexPath:indexPath];
                }];
            }
            [cell showInfo:dict key:sectionTitle indexPath:indexPath];
            return cell;
        }
        
    }else
    {
        id tag =[self.categoryDictionary objectForKey:sectionTitle];
        if(tag && [tag isKindOfClass:[NSDictionary class]])
        {
            NSMutableArray *numArray = [NSMutableArray arrayWithArray:[tag allKeys]];
            [numArray sortUsingComparator: ^NSComparisonResult (NSString *str1, NSString *str2) {
                NSNumber *number1 = [NSNumber numberWithInteger:str1.floatValue];
                NSNumber *number2 = [NSNumber numberWithInteger:str2.floatValue];
                return [number1 compare:number2];
            }];
            if ([numArray containsObject:@"99999999"]) {
                int end = [[NSString stringWithFormat:@"%@",[[tag  objectForKey:@"99999999"] objectForKey:@"end"]] intValue];
                if (end==1) {
                    [numArray removeObject:@"99999999"];
                }
            }
            NSString* kv =[numArray objectAtIndex:indexPath.row];
            if ([kv isEqualToString:@"99999999"]) {
                if(cell == nil)
                {
                    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
                    cell.contentView.backgroundColor = [UIColor clearColor];
                    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
                    cell.selectedBackgroundView.backgroundColor =UIColorFromRGB(0xf4f4f4);
                }
                cell.textLabel.textAlignment =NSTextAlignmentCenter;
                cell.textLabel.text =[NSString stringWithFormat:@"%@",[[tag  objectForKey:@"99999999"] objectForKey:@"end_msg"]];
                cell.textLabel.font = FONT(16);
                cell.textLabel.textColor=UIColorFromRGB(0x285790);
                return cell;
            }else{
                
                NSDictionary* dict =[tag objectForKey:kv];
                ProductCellV1 *cell = (ProductCellV1*)[tableView  dequeueReusableCellWithIdentifier:@""];
                if(cell == nil)
                {
                    cell = [[ProductCellV1 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
                    
                    __weak typeof (self) wSelf = self;
                    [cell setCallBack:^(BEventType t, UIView *v,id obj ,id key,NSIndexPath *indxPath) {
                        [wSelf cellAction:t views:v obj:obj key:key indexPath:indexPath];
                    }];
                }
                [cell showInfo:dict key:sectionTitle indexPath:indexPath];
                return cell;
            }
        }
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSString *sectionTitle =[self.sectionsArray objectAtIndex:indexPath.section];
    
    if ([sectionTitle isEqualToString:@"others"])
    {
        BangDanListCell *cell = (BangDanListCell *)[tableView cellForRowAtIndexPath:indexPath];
        
        GoodViewController *good =[[GoodViewController alloc] init];
        good.type =-1;
        good.dictionary = cell.data;
        good.photo_id = [NSString stringWithFormat:@"%@",[cell.data objectForKey:@"photo_id"]];
        [self.navigationController pushViewController:good animated:YES];
        
    }else if(![sectionTitle isEqualToString:@"search"]&&![sectionTitle isEqualToString:@"cate_tags"]&&![sectionTitle isEqualToString:@"cate_brands"])
    {
        id tag =[self.categoryDictionary objectForKey:sectionTitle];
        if(tag && [tag isKindOfClass:[NSDictionary class]])
        {
            NSMutableArray *numArray = [NSMutableArray arrayWithArray:[tag allKeys]];
            [numArray sortUsingComparator: ^NSComparisonResult (NSString *str1, NSString *str2) {
                NSNumber *number1 = [NSNumber numberWithInteger:str1.floatValue];
                NSNumber *number2 = [NSNumber numberWithInteger:str2.floatValue];
                return [number1 compare:number2];
            }];
            if ([numArray containsObject:@"99999999"]) {
                int end = [[NSString stringWithFormat:@"%@",[[tag  objectForKey:@"99999999"] objectForKey:@"end"]] intValue];
                if (end==1) {
                    [numArray removeObject:@"99999999"];
                }
            }
            NSString* kv =[numArray objectAtIndex:indexPath.row];
            
            if ([kv isEqualToString:@"99999999"])
            {
                NSString *cate =[NSString stringWithFormat:@"%@",[[tag  objectForKey:@"99999999"] objectForKey:@"next"]];
                if ([cate isEqualToString:@"cate"]) {
                    NSString *cateid =[NSString stringWithFormat:@"%@",[[tag objectForKey:@"99999999"] objectForKey:@"next_id"]];
                    NSString *catename =[NSString stringWithFormat:@"%@",[[tag objectForKey:@"99999999"] objectForKey:@"next_name"]];
                    GoodClassViewController *cate =[[GoodClassViewController alloc] init];
                    cate.cateName =catename;
                    cate.cateCid =cateid;
                    cate.cateTag =@"";
                    [self.navigationController pushViewController:cate animated:YES];
                }else
                {
                    [self moreWithTag:sectionTitle];
                }
                
            }else{
                NSDictionary* dict =[tag objectForKey:kv];
                
                GoodViewController *good =[[GoodViewController alloc] init];
                good.type =-1;
                good.dictionary = dict;
                good.photo_id = [NSString stringWithFormat:@"%@",[dict objectForKey:@"photo_id"]];
                [self.navigationController pushViewController:good animated:YES];
            }
        }
        
        
    }
}

-(void)cellAction:(BEventType)type views:(UIView *)v obj:(id)obj key:(id)key indexPath:(NSIndexPath *)indxPath
{
    
    switch (type) {
        case BEventType_category:
        {
            GoodClassViewController *cate =[[GoodClassViewController alloc] init];
            cate.cateName =[NSString stringWithFormat:@"%@",[obj objectForKey:@"cat2"]];
            cate.cateCid =[NSString stringWithFormat:@"%@",[obj objectForKey:@"cat2_id"]];
            cate.cateTag =[NSString stringWithFormat:@"%@",key];
            [self.navigationController pushViewController:cate animated:YES];
        }
            break;
        case BEventType_tagBrand:
        {
            if ([[NSString stringWithFormat:@"%@",[obj objectForKey:@"name"]] isEqualToString:@"更多"]) {
                return;
            }
            GoodBrandViewController *brand =[[GoodBrandViewController alloc] init];
            brand.cId =  @"";
            brand.brandName =[NSString stringWithFormat:@"%@",[obj objectForKey:@"name"]];
            brand.brandId =[NSString stringWithFormat:@"%@",[obj objectForKey:@"id"]];
            [self.navigationController pushViewController:brand animated:YES];
            
        }
            break;
        case BEventType_brand:
        {
            GoodBrandViewController *brand =[[GoodBrandViewController alloc] init];
            brand.brandName =[NSString stringWithFormat:@"%@",[obj objectForKey:@"brand"]];
            brand.brandId =[NSString stringWithFormat:@"%@",[obj objectForKey:@"brand_id"]];
            brand.cId =[NSString stringWithFormat:@"%@",[obj objectForKey:@"cat2_id"]];
            [self.navigationController pushViewController:brand animated:YES];
        }
            break;
        case BEventType_good:
        {
            GoodViewController *good =[[GoodViewController alloc] init];
            good.type =-1;
            good.dictionary = obj;
            good.photo_id = [NSString stringWithFormat:@"%@",[obj objectForKey:@"photo_id"]];
            [self.navigationController pushViewController:good animated:YES];
        }
            break;
        case BEventType_comment:
        {
            NSString *pid =[obj objectForKey:@"photo_id"];
            CommentViewController *comm =[[CommentViewController alloc] init];
            comm.photo_id =pid;
            comm.dictionary =obj;
            [self.navigationController pushViewController:comm animated:YES];
        }
            break;
        case BEventType_like:
        {
            if ([self isLogin]) {
                NSString *pid =[NSString stringWithFormat:@"%@",[obj objectForKey:@"photo_id"]];
                [self likeWithPid:pid key:key indexPath:indxPath];
            }
        }
            break;
        case BEventType_tag:
            
            break;
        case BEventType_categoryTag:
        {
            NSDictionary *dict =(NSDictionary*)obj;
            self.currentPage=0;
            self.isSelectedTag =YES;
            int selected =[[NSString stringWithFormat:@"%@",[dict objectForKey:@"selected"]] intValue];
            if (selected==0) {
                [self getCategoryDataWithCid:self.cateCid tagstr:[NSString stringWithFormat:@"%@",[dict objectForKey:@"name"]]];
            }else
            {
                [self getCategoryDataWithCid:self.cateCid tagstr:@""];
            }
            
        }
            break;
        case BEventType_search:
        {
            SearchViewController *search =[[SearchViewController alloc] init];
            [self.navigationController pushViewController:search animated:YES];
        }
            break;
        case BEventType_bangdanTop:
        {
            NSString *pid =[NSString stringWithFormat:@"%@",[obj objectForKey:@"photo_id"]];
            if ([self isLogin]) {
                [self pushTopWithPid:pid key:key indexPath:indxPath];
            }
        }
            break;
        case BEventType_bangdanDelDanpin:
        {
            //            UIActionSheet *actionSheet = [[UIActionSheet alloc]
            //                                          initWithTitle:nil
            //                                          delegate:self
            //                                          cancelButtonTitle:@"取消"
            //                                          destructiveButtonTitle:nil
            //                                          otherButtonTitles:@"找人来顶",@"评论",nil];
            //            actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
            //            [actionSheet showInView:self.view];
        }
            break;
            
        default:
            break;
    }
}


#pragma mark -----
-(void)getCategoryDataWithCid:(NSString*)cat2_id tagstr:(NSString *)tagstr
{
    NSMutableDictionary *dict =[NSMutableDictionary dictionary];
    [dict setValue:@"cate" forKey:@"action"];
    [dict setValue:@"" forKey:@"user_id"];
    [dict setValue:cat2_id forKey:@"cate_id"];
    [dict setValue:@"" forKey:@"brand_id"];
    [dict setValue:tagstr forKey:@"tag"];
    [dict setValue:@"" forKey:@"sku_name"];
    [dict setValue:[NSString stringWithFormat:@"%d",self.currentPage] forKey:@"from"];
    [dict setValue:@"1" forKey:@"grid"];
    [dict setValue:@"" forKey:@"lat"];
    [dict setValue:@"" forKey:@"lon"];
    [dict setValue:self.word forKey:@"word"];
    [dict setValue:self.mword forKey:@"mword"];
    self.isRequesting =YES;
    __weak typeof (self) wSelf = self;
    [BaseModel wareList:dict  allowFetchCache:NO  success:^(AFHTTPRequestOperation *operation, NSObject *result) {
        wSelf.isRequesting =NO;
        [wSelf parserResultObject:result];
        [wSelf.tbView.infiniteScrollingView stopAnimating];
        [[ZToastManager ShardInstance] hideprogress];
  //      [ZShare hiddenAnimationView:wSelf.view];
    } failure:^(NSError *error) {
        wSelf.isRequesting =NO;
        [ZShare hiddenAnimationView:wSelf.view text:@""];
        [wSelf.tbView.infiniteScrollingView stopAnimating];
 //       [[ZToastManager ShardInstance] hideprogress];
    }];
    
}

-(void)parserResultObject:(id)resultObject
{
    if (resultObject==nil) {
        return;
    }
    if (resultObject && [resultObject isKindOfClass:[NSDictionary class]]) {
        //
        if (self.currentPage==0) {
            [self.sectionsArray removeAllObjects];
            self.sectionsArray =[NSMutableArray arrayWithArray:@[@"search",@"cate_tags",@"cate_brands",@"others"]];
            //
            NSMutableArray *sortTempArray =[NSMutableArray array];
            id cate_tags =  [resultObject objectForKey:@"cate_tags"];
            if (cate_tags && [cate_tags isKindOfClass:[NSArray class]]){
                [_tagsArray removeAllObjects];
                for (NSDictionary *di in cate_tags) {
                    if (di && [di isKindOfClass:[NSDictionary class]])
                    {
                        [self.tagsArray addObject:di];
                        [sortTempArray addObject:[di objectForKey:@"name"]];
                    }
                }
                if ([self.tagsArray count]==0) {
                    [self.sectionsArray removeObject:@"cate_tags"];
                }
            }else
            {
                if (self.currentPage==0 || [self.tagsArray count]==0) {
                    [self.sectionsArray removeObject:@"cate_tags"];
                }
            }
            //
            id cate_brands =  [resultObject objectForKey:@"cate_brands"];
            if (cate_brands && [cate_brands isKindOfClass:[NSArray class]]){
                [_brandsArray removeAllObjects];
                for (NSDictionary *di in cate_brands) {
                    if (di && [di isKindOfClass:[NSDictionary class]])
                    {
                        [self.brandsArray addObject:di];
                    }
                }
                if ([self.brandsArray count]==0) {
                    [self.sectionsArray removeObject:@"cate_brands"];
                }
            }else
            {
                if (self.currentPage==0 || [self.brandsArray count]==0) {
                    [self.sectionsArray removeObject:@"cate_brands"];
                }
            }
            
            //
            id data =[resultObject objectForKey:@"data"];
            if (data && [data isKindOfClass:[NSDictionary class]]) {
                int scount =(int)[sortTempArray count];
                if(scount>0)
                {
                    for (int i =0 ;i<scount ;i++) {
                        NSString *tagName =[sortTempArray objectAtIndex:i];
                        for (NSString *datakey in [data allKeys])
                        {
                            if ([datakey isEqualToString:tagName]) {
                                [self.sectionsArray insertObject:datakey atIndex:[self.sectionsArray count]-1];
                                break;
                            }
                        }
                    }
                }else{
                    for (NSString *datakey in [data allKeys])
                    {
                        [self.sectionsArray insertObject:datakey atIndex:[self.sectionsArray count]-1];
                    }
                }
                
                [self.categoryDictionary addEntriesFromDictionary:data];
            }else
            {
                [self.categoryDictionary removeAllObjects];
            }
            //
            id others =[resultObject objectForKey:@"others"];
            
            if (others && [others isKindOfClass:[NSArray class]]) {
                if (self.currentPage==0) {
                    [self.othersArray removeAllObjects];
                }
                for (NSDictionary *dict in others) {
                    [self.othersArray addObject:dict];
                }
            }else
            {
                if (self.currentPage==0|| [self.othersArray count]==0) {
                    [self.sectionsArray removeObject:@"others"];
                }
            }
            
            
        }else
        {
            id others =[resultObject objectForKey:@"others"];
            
            if (others && [others isKindOfClass:[NSArray class]]) {
                for (NSDictionary *dict in others) {
                    [self.othersArray addObject:dict];
                }
            }
        }
        //
        //end
        self.isLoadFinished =  [[resultObject objectForKey:@"end"] boolValue];
       
        if (self.isLoadFinished) {
            [self.tbView.infiniteScrollingView stopAnimating];
            [self.tbView setShowsInfiniteScrolling:NO];
            NSString *end_msg =[resultObject objectForKey:@"end_msg"];
            self.tbView.tableFooterView =[ZShare footerView:end_msg];
        }
        
    }
    [_tbView reloadData];
    
}
//喜欢
-(void)likeWithPid:(NSString*)pid key:(NSString*)key indexPath:(NSIndexPath*)indexPath
{
    [UMengShare event:CK_LOVE acc:1];
    __weak typeof (self) wSelf = self;
    NSMutableDictionary *dict =[NSMutableDictionary dictionary];
    [dict setValue:pid forKey:@"photo_id"];
    [BaseModel love:dict success:^(AFHTTPRequestOperation *operation, id result) {
        int code =  [[result objectForKey:@"code"] intValue];
        NSString *like_cnt =  [NSString stringWithFormat:@"%@",[result objectForKey:@"like_cnt"]];
        if (code==1 || code==2)
        {
            if ([key isEqualToString:@"others"]) {
                for (int i=0; i<[wSelf.othersArray count]; i++) {
                    NSDictionary *dict =[wSelf.othersArray objectAtIndex:i];
                    if ([dict isKindOfClass:[NSDictionary class]]) {
                        NSString *pidd = [NSString stringWithFormat:@"%@",[dict objectForKey:@"photo_id"]];
                        if ([pidd isEqualToString:pid]) {
                            NSMutableDictionary *temp = [dict mutableCopy];
                            if (code==1) {
                                [temp setValue:@"1" forKey:@"like"];
                            }else{
                                [temp setValue:@"0" forKey:@"like"];
                            }
                            [temp setValue:like_cnt forKey:@"like_cnt"];
                            [wSelf.othersArray replaceObjectAtIndex:i withObject:temp];
                            break;
                        }
                    }
                }
            }else{
                [wSelf.categoryDictionary enumerateKeysAndObjectsUsingBlock:^(id key,id value,BOOL *stop){
                    id keyArray = [[wSelf.categoryDictionary objectForKey:key] mutableCopy];
                    if(keyArray && [keyArray isKindOfClass:[NSDictionary class]])
                    {
                        NSArray *keyArry =[keyArray allKeys];
                        keyArry= [keyArry sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
                        NSMutableArray *key1 =[NSMutableArray arrayWithArray:keyArry];
                        if ([key1 containsObject:@"99999999"]) {
                            [key1 removeObject:@"99999999"];
                        }
                        
                        for(int i =0 ;i<[key1 count];i++)
                        {
                            NSString *kv =[key1 objectAtIndex:i];
                            NSDictionary *dict =[keyArray objectForKey:kv];
                            NSString *pidd = [NSString stringWithFormat:@"%@",[dict objectForKey:@"photo_id"]];
                            if ([pidd isEqualToString:pid]) {
                                NSMutableDictionary *temp = [dict mutableCopy];
                                if (code==1) {
                                    [temp setValue:@"1" forKey:@"like"];
                                }else{
                                    [temp setValue:@"0" forKey:@"like"];
                                }
                                [temp setValue:like_cnt forKey:@"like_cnt"];
                                [keyArray setValue:temp forKey:kv];
                                
                                break;
                            }
                        }
                    }
                    [self.categoryDictionary setValue:keyArray forKey:key];
                }];
            }
            [[NSNotificationCenter defaultCenter] postNotificationName:@"updateLoveList" object:pid];
            
            [_tbView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
            
        }
        
    } failure:^(NSError *error) {
//        [ZShare hiddenAnimationView:wSelf.view text:@""];
    }];
    
}
//更多
-(void)moreWithTag:(NSString *)tagstr
{
    if (![tagstr isEqualToString:self.cateTag]) {
        self.morePage =0;
    }
    self.cateTag = tagstr;
    NSDictionary *dictk =[self.categoryDictionary objectForKey:tagstr];
    int from =(int)[[dictk allKeys] count]-1;
    
    NSMutableDictionary *dict =[NSMutableDictionary dictionary];
    [dict setValue:@"cate" forKey:@"action"];
    [dict setValue:@"" forKey:@"user_id"];
    [dict setValue:self.cateCid forKey:@"cate_id"];
    [dict setValue:@"" forKey:@"brand_id"];
    [dict setValue:self.cateTag forKey:@"tag"];
    [dict setValue:@"" forKey:@"sku_name"];
    [dict setValue:[NSString stringWithFormat:@"%d",from] forKey:@"from"];
    [dict setValue:@"1" forKey:@"grid"];
    [dict setValue:@"" forKey:@"lat"];
    [dict setValue:@"" forKey:@"lon"];
    self.isRequesting =YES;
    __weak typeof (self) wSelf = self;
    [BaseModel wareList:dict  allowFetchCache:NO  success:^(AFHTTPRequestOperation *operation, NSObject *result) {
        wSelf.isRequesting =NO;
        [wSelf parserMoreObject:result tag:tagstr];
    } failure:^(NSError *error) {
        wSelf.isRequesting =NO;
        [ZShare hiddenAnimationView:wSelf.view text:@""];
    }];
}
-(void)parserMoreObject:(id)resultObject tag:(NSString*)tag
{
    if (resultObject==nil) {
        return;
    }
    id more =[resultObject objectForKey:@"data"];
    if (more && [more isKindOfClass:[NSDictionary class]]) {
        NSArray *keyk =[more allKeys];
        if ([keyk count]>0) {
            if ([keyk containsObject:tag]) {
                NSMutableDictionary *dictOld = [[self.categoryDictionary objectForKey:tag] mutableCopy];
                NSDictionary* d = [more objectForKey:tag];
                [d enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
                    [dictOld setValue:obj forKey:key];
                }];
                [self.categoryDictionary setValue:dictOld forKey:tag];
            }
        }
    }
    [_tbView reloadData];
}
-(void)notificationManager
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(updateLoveList:)
                                                 name:@"updateLoveList"
                                               object:nil];
}
-(void)updateLoveList:(NSNotification*)notification
{
    DLog(@"%@",notification.object);
    self.currentPage = 0;
    [self getCategoryDataWithCid:self.cateCid tagstr:self.cateTag];
}


-(void)commentAction
{
    CommentViewController *comm =[[CommentViewController alloc] init];
    //    comm.photo_id =self.photo_id;
    comm.fromType =@"1";
    [self.navigationController pushViewController:comm animated:YES];
}
-(void)pushTopWithPid:(NSString*)pid key:(NSString*)key indexPath:(NSIndexPath*)indexPath
{
    
    __weak typeof (self) wSelf = self;
    NSMutableDictionary *dict =[NSMutableDictionary dictionary];
    [dict setValue:self.cateCid forKey:@"cate_id"];
    [dict setValue:pid forKey:@"photo_id"];
    [BaseModel categoryPushTop:dict success:^(AFHTTPRequestOperation *operation, id result) {
        int code =  [[result objectForKey:@"code"] intValue];
        NSString *like_cnt =  [NSString stringWithFormat:@"%@",[result objectForKey:@"ware_push_top"]];
        if (code==1 || code==2)
        {
            if ([key isEqualToString:@"others"]) {
                for (int i=0; i<[wSelf.othersArray count]; i++) {
                    NSDictionary *dict =[wSelf.othersArray objectAtIndex:i];
                    if ([dict isKindOfClass:[NSDictionary class]]) {
                        NSString *pidd = [NSString stringWithFormat:@"%@",[dict objectForKey:@"photo_id"]];
                        if ([pidd isEqualToString:pid]) {
                            NSMutableDictionary *temp = [dict mutableCopy];
                            if (code==1) {
                                [temp setValue:@"1" forKey:@"had_ware_push_top"];
                            }else{
                                [temp setValue:@"0" forKey:@"had_ware_push_top"];
                            }
                            [temp setValue:like_cnt forKey:@"ware_push_top"];
                            [wSelf.othersArray replaceObjectAtIndex:i withObject:temp];
                            break;
                        }
                    }
                }
            }else{
                [wSelf.categoryDictionary enumerateKeysAndObjectsUsingBlock:^(id key,id value,BOOL *stop){
                    id keyArray = [[wSelf.categoryDictionary objectForKey:key] mutableCopy];
                    if(keyArray && [keyArray isKindOfClass:[NSDictionary class]])
                    {
                        NSArray *keyArry =[keyArray allKeys];
                        keyArry= [keyArry sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
                        NSMutableArray *key1 =[NSMutableArray arrayWithArray:keyArry];
                        if ([key1 containsObject:@"99999999"]) {
                            [key1 removeObject:@"99999999"];
                        }
                        
                        for(int i =0 ;i<[key1 count];i++)
                        {
                            NSString *kv =[key1 objectAtIndex:i];
                            NSDictionary *dict =[keyArray objectForKey:kv];
                            NSString *pidd = [NSString stringWithFormat:@"%@",[dict objectForKey:@"photo_id"]];
                            if ([pidd isEqualToString:pid]) {
                                NSMutableDictionary *temp = [dict mutableCopy];
                                if (code==1) {
                                    [temp setValue:@"1" forKey:@"had_ware_push_top"];
                                }else{
                                    [temp setValue:@"0" forKey:@"had_ware_push_top"];
                                }
                                [temp setValue:like_cnt forKey:@"ware_push_top"];
                                [keyArray setValue:temp forKey:kv];
                                
                                break;
                            }
                        }
                    }
                    [self.categoryDictionary setValue:keyArray forKey:key];
                }];
            }
            [[NSNotificationCenter defaultCenter] postNotificationName:@"updateLoveList" object:pid];
            
            [_tbView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
            
        }
        
    } failure:^(NSError *error) {
        [ZShare hiddenAnimationView:wSelf.view text:@""];
    }];
    
}
@end
